package com.ilop.sthome.ui.adapter.scene;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.utils.CoderUtils;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Author skygge.
 * @Date on 2019-10-24.
 * @Dec:
 */
public class TriggerInAdapter extends RecyclerView.Adapter<TriggerInAdapter.ItemHolder> {

    private Context mContext;
    private List<DeviceInfoBean> mList;
    private boolean isEditable = true;
    private boolean isFirstDefault = false; // 是否常用功能第一个输入设备

    public TriggerInAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setList(List<DeviceInfoBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void setEditable(boolean editable) {
        this.isEditable = editable;
    }

    public void setFirstDefault(boolean firstDefault) {
        this.isFirstDefault = firstDefault;
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_trigger, viewGroup, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        DeviceInfoBean device = mList.get(i);
        itemHolder.mName.setTextColor(isEditable ? ContextCompat.getColor(mContext, R.color.text_title) : ContextCompat.getColor(mContext, R.color.text_normal));
        itemHolder.mAction.setVisibility(View.VISIBLE);
        itemHolder.mImg.setImageResource(SmartDevice.getType(device.getDevice_type()).getDrawableResId());
        if(TextUtils.isEmpty(device.getSubdeviceName())){
            if(SmartDevice.EE_SIMULATE_CLICK==SmartDevice.getType(device.getDevice_type())){
                itemHolder.mName.setText(mContext.getString(SmartDevice.getType(device.getDevice_type()).getTypeStrId()));
            }else {
                String name = mContext.getString(SmartDevice.getType(device.getDevice_type()).getTypeStrId())+device.getDevice_ID();
                itemHolder.mName.setText(name);
            }
        }else{
            itemHolder.mName.setText(device.getSubdeviceName());
        }

        if (TextUtils.isEmpty(device.getDevice_type())) {
            itemHolder.mAction.setText(mContext.getString(R.string.device_not_exist));
        }else {
            SmartDevice smartDevice = SmartDevice.getType(device.getDevice_type());
            switch (smartDevice){
                case EE_DEV_TH_CHECK:
                    itemHolder.mAction.setText(CoderUtils.getHumitureMsg(mContext, device.getDevice_status()));
                    break;
                case EE_DEV_BUTTON:
                    itemHolder.mAction.setText(mContext.getString(R.string.press));
                    break;
                case EE_DEV_MODE_BUTTON:
                    if ("40".equals(device.getDevice_status())){
                        itemHolder.mAction.setText(mContext.getString(R.string.custom_button_push));
                    }else {
                        itemHolder.mAction.setText(mContext.getString(R.string.help));
                    }
                    break;
                case EE_SIMULATE_TIMER:
                    String init_week = device.getDevice_status().substring(2, 4);
                    String week = CoderUtils.getWeekInfo(mContext, init_week);
                    int hour = Integer.parseInt(device.getDevice_status().substring(4, 6), 16);
                    int min = Integer.parseInt(device.getDevice_status().substring(6), 16);
                    String time = CoderUtils.setCodeSupplement1Byte(String.valueOf(hour))
                            + ":"+ CoderUtils.setCodeSupplement1Byte(String.valueOf(min)) ;
                    itemHolder.mAction.setText(week);
                    itemHolder.mName.setText(time);
                    break;
                case EE_SIMULATE_CLICK:
                    itemHolder.mAction.setText(mContext.getString(R.string.click_to_execute_to_execute_the_action));
                    break;
                default:
                    itemHolder.mAction.setText(CoderUtils.getDeviceTypeMsg(mContext, device.getDevice_type(), device.getDevice_status()));
                    break;
            }
        }
        itemHolder.itemView.setEnabled(isEditable);
        if (isFirstDefault && i == 0){
            itemHolder.mName.setTextColor(ContextCompat.getColor(mContext, R.color.text_normal));
            itemHolder.itemView.setEnabled(false);
            if (!TextUtils.isEmpty(device.getDevice_type())){
                SmartDevice smartDevice = SmartDevice.getType(device.getDevice_type());
                if (smartDevice == SmartDevice.EE_DEV_TH_CHECK){
                    itemHolder.mName.setTextColor(ContextCompat.getColor(mContext, R.color.text_title));
                    itemHolder.itemView.setEnabled(true);
                }
            }
        }
        itemHolder.itemView.setOnClickListener(view -> LiveDataBus.get().with(CommonId.INPUT_CLICK).setValue(i));

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_device_delay)
        TextView mDelay;
        @BindView(R.id.item_trigger_img)
        ImageView mImg;
        @BindView(R.id.item_trigger_name)
        TextView mName;
        @BindView(R.id.item_trigger_action)
        TextView mAction;
        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
